import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex)

export default new Vuex.Store({
    // 定义应用程序的状态
    state: {
        collapse: false,
        tabList: [{name: "首页", path: "/"}],
        userId: null,
        username: null,
        headPortrait: null,
        nickname: null,
        email: null,
        introduce: null,
        accessToken: null,
        refreshToken: null,
        userMenuList: [],
        encryData:{}
    },

    // 从state中派生出新的状态，类似于计算属性
    getters: {
        getEncryData(state) {
            return state.encryData
        }
    },

    // 修改state中的状态，必须是同步函数
    mutations: {

        //保存当前页标签
        saveTab(state, tab) {
            if (state.tabList.findIndex(item => item.path === tab.path) === -1) {
                state.tabList.push({name: tab.name, path: tab.path});
            }
        },

        //删除标签
        removeTab(state, tab) {
            const index = state.tabList.findIndex(item => item.name === tab.name);
            state.tabList.splice(index, 1);
        },

        //关闭所有标签页
        resetTab(state) {
            state.tabList = [{name: "首页", path: "/"}];
        },

        //折叠按钮
        trigger(state) {
            state.collapse = !state.collapse;
        },

        //登陆时保存用户信息
        login(state, user) {
            state.userId = user.id;
            state.username = user.username;
            state.roleList = user.roleIds;
            state.headPortrait = user.headPortrait;
            state.nickname = user.nickname;
            state.email = user.email;
            state.accessToken = user.accessToken;
            state.refreshToken = user.refreshToken;
        },

        //保存用户菜单列表
        saveUserMenuList(state, userMenuList) {
            state.userMenuList = userMenuList;
        },

        //退出登陆时清空用户信息
        logout(state) {
            state.userId = null;
            state.headPortrait = null;
            state.username = null;
            state.nickname = null;
            state.email = null;
            state.accessToken = null;
            state.refreshToken = null;
            state.userMenuList = [];
            state.tabList = [];
        },

        //更改用户信息
        updateUserInfo(state, user) {
            state.headPortrait = user.headPortrait;
            state.nickname = user.nickname;
            state.email = user.email;
            state.introduce = user.introduce;
        },

        //刷新token
        updateTokens(state, tokens) {
            state.accessToken = tokens.accessToken;
            state.refreshToken = tokens.refreshToken;
        },

        // 记住密码
        encrySave(state, encryDate) {
            state.encryData = encryDate;
        },

        // 忘记密码
        encryRemove(state) {
            state.encryData = {};
        }
    },

    // 异步操作状态，可以包含任意异步操作
    actions: {},

    // 将store分割成模块，每个模块拥有自己的state、mutations、getters和actions
    modules: {},
    
    plugins: [
        createPersistedState()
    ]
})
